// pages/main/function.js

var interval = null //倒计时函数

Page({
  /**
   * 页面的初始数据
   */
  data: {
    tbodyHeight: 10,
    time: "获取验证码",
    disabled: false,
    currentTime: 60,

    // 模块分类
    moduleFuntion: [{
      id: "id0",
      name: "模块一"
    }, {
      id: "id1",
      name: "模块二"
    }, {
      id: "id2",
      name: "模块三"
    }],

    // 模块详情
    funtionDetail: [{
      id: "id0",
      name: "模块一",
      list: [{
        id: "FT0",
        name: "扫描二维码",
        detail: "扫描二维码识别二维码内容"
      }, {
          id: "FT1",
          name: "查快递",
          detail: "查询各大平台快递进度"
        }, {
          id: "FT2",
          name: "地图组件",
          detail: "玩转 自定义地图"
        }]
    }, {
      id: "id1",
      name: "模块二",
      list: [{
        id: "FT0",
        name: "客服",
        detail: "小程序客服聊天界面"
      }, {
          id: "FT1",
          name: "加载webView",
          detail: "加载WEBVIew可以实现长按识别二维码"
        }]
    }, {
      id: "id2",
      name: "模块三",
      list: [{
        id: "id2",
        name: "每日一笑",
        detail: "笑话列表"
      }, {
        id: "id2",
        name: "网络测试",
        detail: "网络测试具体测试什么我也不清楚"
      }, {
        id: "id2",
        name: "查看wifi信息",
        detail: "查看wifi信息"
      }, {
        id: "id2",
        name: "分享",
        detail: "分享界面"
      }, {
        id: "id2",
        name: "获取验证码",
        detail: "点击按钮，获取验证码效果"
      }]
    }],
    toView: "id0"
  },

  moduleClick: function(e) {
    console.log('功能模块')
    console.log(e)
    this.setData({
      toView: e.currentTarget.id
    })
    console.log(e.currentTarget.id)
  },
  detailClick: function(e) {
    console.log('模块详情')
    console.log(e)
    switch (e.currentTarget.dataset.section) {
      case 0:
        console.log('第一个区 第' + e.currentTarget.dataset.row + '行');
        if (e.currentTarget.dataset.row == 0) {
          this.ScanClick()
        } else if (e.currentTarget.dataset.row == 2){
          wx.navigateTo({
            url: '../MapComponent/MapComponent',
          })
        }
        break;
      case 1:
        console.log('第二个区 第' + e.currentTarget.dataset.row + '行');
        if (e.currentTarget.dataset.row == 1) {
          wx.navigateTo({
            url: '../WEB/webView',
          })
        }
        break;
      case 2:
        console.log('第三个区 第' + e.currentTarget.dataset.row + '行');
        if (e.currentTarget.dataset.row == 0) {
          wx.navigateTo({
            url: '../joke/joke',
          })
        } else if (e.currentTarget.dataset.row == 2) {
          this.bindWifiInfo()
        } else if (e.currentTarget.dataset.row == 1) {
          this.networkTestClick()
        } else if (e.currentTarget.dataset.row == 3) {
          wx.navigateTo({
            url: '../share/sharePage',
          })
        } else if (e.currentTarget.dataset.row == 4) {
          this.getVerificationCode()
        }
        break;

      default:
        console.log('什么鬼👻');
    }
  },

  weatherClick: function() {

  },

  CourierClick: function() {

  },

  //验证码倒计时函数
  getVerificationCode() {
    this.getCode();
    var that = this
    that.setData({
      disabled: true
    })
  },

  getCode: function(options) {
    var that = this;
    var currentTime = that.data.currentTime;
    that.setData({
      time: currentTime + '秒'
    })
    interval = setInterval(function() {
      currentTime--;
      that.setData({
        time: currentTime + '秒'
      })

      //验证码倒计时结束
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '重新获取',
          currentTime: 60,
          disabled: false
        })
      }
    }, 1000)
  },

  networkTestClick: function() {
    wx.login({
      success: function(res) {
        console.log('登陆成功' + res.code)
        //发起网络请求
        wx.request({
          url: 'http://localhost:8181/Login',
          method: 'post',
          data: {
            code: res.code
          },
          success: function(obj) {
            console.log("服务器返回 " + obj.data.token)
            if (obj.data.token.length > 0) {
              wx.showToast({
                title: '登陆成功',
              })
            }
          }
        })
      }
    })

    // wx.getUserInfo({
    //   // lang: "zh_CN",
    //   timeout: 25,
    //   success: function(res){
    //     console.log(res+'用户名' + res.userInfo.nickName)
    //   },
    //   fail: function(error){
    //     console.log(error)
    //       wx.showToast({
    //         title: '获取用户信息失败',
    //       })
    //   }
    // })
  },

  bindWifiInfo: function(e) {
    wx.getNetworkType({
      success: res => {
        console.log(res)
      }
    })

  },

  kefuxiaoxi(e) {
    console.log(e.path)
    console.log(e.query)
  },
  /**
   * 扫描二维码
   * @constructor
   */
  ScanClick: function() {
    wx.scanCode({
      success: function(res) {
        console.log(res.result)
      },
      fail: function() {
        console.log('扫描失败')
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.zhjgetheight()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  // 一种解决 获取不到屏幕高度的问题
  zhjgetheight: function() {
    var that = this
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          tbodyHeight: res.windowHeight
        })
        console.log(res)
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})